

#ifndef SOCKETMGR_H_WIN32
#define SOCKETMGR_H_WIN32
#include "math/AlignedObjectArray.h"

namespace Apo
{
#ifdef CONFIG_USE_Apo


class Socket;
class SERVER_DECL SocketMgr /*: public ApoSingleton<SocketMgr>*/
{
public:
	SocketMgr();
	~SocketMgr();
	//static SocketMgr * gSocketMgr;


	inline HANDLE GetCompletionPort() { return m_completionPort; }
	void SpawnWorkerThreads();
	void CloseAll();
	void AddSocket(Socket * s)
	{
		socketLock.Acquire();
		_sockets.push_back(s);//  insert(s);
		socketLock.Release();
	}

	void RemoveSocket(Socket * s)
	{
		socketLock.Acquire();
		_sockets.erase(s);// erase(s);
		socketLock.Release();
	}

	void ShutdownThreads();
	long threadcount;

private:
	HANDLE			m_completionPort;
	AlignedObjectArray<Socket*>	_sockets;
	Mutex			socketLock;
};

extern SocketMgr	*		 gSocketMgr ;

typedef void(*OperationHandler)(Socket * s, uint32 len);

class SocketWorkerThread : public ThreadBase
{
public:
	void run();
};

void SERVER_DECL HandleReadComplete(Socket * s, uint32 len);
void SERVER_DECL HandleWriteComplete(Socket * s, uint32 len);
void SERVER_DECL HandleShutdown(Socket * s, uint32 len);

static OperationHandler ophandlers[NUM_SOCKET_IO_EVENTS] = {
	&HandleReadComplete,
	&HandleWriteComplete,
	&HandleShutdown };

#endif
}
#endif